package com.wkbb.sports.controller;

import com.github.pagehelper.PageInfo;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.GradeDto;
import com.wkbb.common.dto.basic.OrganizationDto;
import com.wkbb.common.enums.OriginTypeEnum;
import com.wkbb.common.utils.GradeUtil;
import com.wkbb.sports.dto.ExamineDto;
import com.wkbb.sports.feign.BasicFeignService;
import com.wkbb.sports.service.ExamineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(description = "体检管理")
@RequestMapping("/sports/{apiVersion}/examine")
@Slf4j
public class ExamineController {

    @Autowired
    private ExamineService examineService;

    @Autowired
    private BasicFeignService basicFeignService;

    @ApiOperation(value = "查询体检项目分页列表")
    @RequestMapping(value = "getExaminePageList", method = RequestMethod.GET)
    public ResultDTO<PageInfo<ExamineDto>> getExaminePageList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "年级ID", required = false) @RequestParam(name = "gradeId", required = false) Long gradeId,
            @ApiParam(value = "性别 (0全部 1男 2女)", required = false) @RequestParam(name = "sex", required = false) Long sex,
            @ApiParam(value = "页码", required = true, defaultValue = "1") @RequestParam(name = "pageNo", required = true, defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页条数", required = true, defaultValue = "10") @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer pageSize) {
        log.info("getExaminePageList orgId:{} gradeId:{} sex{} pageNo:{} pageSize:{}", orgId, gradeId, sex,pageNo, pageSize);
        try {
            if (orgId == null){
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("orgId", orgId);             //学校id
            params.put("gradeId", gradeId);         //年级id
            params.put("sex", sex);                 //性别
            return new ResultDTO<>(examineService.getExaminePageList(params, pageNo, pageSize));
        } catch (Exception e) {
            log.error("***********ExamineController.getExaminePageList******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "新增体检项目")
    @RequestMapping(value = "/addExamine", method = RequestMethod.POST)
    public ResultDTO addExamine(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @RequestBody ExamineDto examineDto) {
        log.info("addExamine {}",examineDto.toString());
        try {
            if (examineDto.getOrgId() == null){
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            return examineService.addExamine(examineDto);
        }catch (Exception e){
            log.error("***********ExamineController.addExamine******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "体检项目详情")
    @RequestMapping(value = "/getExamine/{examineId}", method = RequestMethod.GET)
    public ResultDTO getExamine(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "体检项目id", required = true) @PathVariable(value = "examineId", required = true) String examineId,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId
    ){
        log.info("getExamine examineId{} orgId{}",examineId,orgId);
        try {
            if (orgId == null || examineId == null){
                return new ResultDTO(ErrorCode.PARAM_MISSING);
            }
            Map<String,Object> params = new HashMap<>();
            params.put("orgId",orgId);
            params.put("examineId",examineId);
            List<ExamineDto> examineDto =  examineService.getExamineById(params);
            return new ResultDTO(examineDto);
        }catch (Exception e){
            log.error("***********ExamineController.getExamine******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "修改体检项目详情")
    @RequestMapping(value = "/updateExamine", method = RequestMethod.PUT)
    public ResultDTO updateExamine(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @RequestBody ExamineDto examineDto){
        log.info("updateExamine {}",examineDto.toString());
        try {
            if (examineDto.getOrgId() == null || examineDto.getId() == null){
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            return examineService.updateExamine(examineDto);
        }catch (Exception e){
            log.error("***********ExamineController.updateExamine******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "删除体检项目")
    @RequestMapping(value = "/delExamine/{examineId}", method = RequestMethod.DELETE)
    public ResultDTO delExamine(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "序号", required = false) @PathVariable("examineId") Integer examineId,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId){
        log.info("delExamine orgId{} examineId{}",orgId,examineId);
        try {
            if (orgId == null || examineId == null){
                return new ResultDTO(ErrorCode.PARAM_MISSING);
            }
            Map<String,Object> params = new HashMap<>();
            params.put("orgId",orgId);
            params.put("examineId",examineId);
            return examineService.delExamine(params);
        }catch (Exception e){
            log.error("***********ExamineController.delExamine******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "初始化学校体检项目")
    @RequestMapping(value = "/initExamine", method = RequestMethod.POST)
    public ResultDTO initExamine(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId)
    {
        log.info("initExamine orgId{}",orgId);
        try {
            if (orgId == null){
                return new ResultDTO(ErrorCode.PARAM_MISSING);
            }

            ResultDTO<OrganizationDto> organizationResult = basicFeignService.getOrgById(orgId);
            if(organizationResult.isSuccess() && null!=organizationResult.getData()) {
                OrganizationDto organizationDto = organizationResult.getData();
                if (organizationDto.getOrigin().equals(OriginTypeEnum.SELF_SUPPORT.getValue().intValue())) {
                    Short gradeType = GradeUtil.getGradeType(organizationDto.getOrgType());
                    ResultDTO<List<GradeDto>> gradeResult = basicFeignService.getGradeList(1L, gradeType.intValue());
                    if (gradeResult.isSuccess() && null != gradeResult.getData()) {
                        List<GradeDto> gradeList = gradeResult.getData();
                        for (GradeDto gradeDto : gradeList) {
                            examineService.copyRule(1, gradeDto.getId(), orgId, gradeDto.getId());
                        }
                    }
                }
            }
            return new ResultDTO(true);
        }catch (Exception e){
            log.error("***********ExamineController.initExamine******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }
}
